package com.talent.jiwen.base;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import com.talent.jiwen.http.common.ActivityLifeCycleEvent;
import com.talent.jiwen.util.MyToast;

import org.greenrobot.eventbus.EventBus;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import rx.subjects.PublishSubject;

/**
 * A simple {@link Fragment} subclass.
 */
public abstract class BaseFragment extends Fragment {

    protected FragmentActivity mActivity;
    public View rootView;
    Unbinder unbinder;

    protected int pageNo = 1; //当前页数
    protected int pageNoMax;  //最大页数
    protected boolean isCheck = true;//刷新还是加载
    protected static final String pageSize = "20";//每页加载页数

    //生命周期控制
    public final PublishSubject<ActivityLifeCycleEvent> lifecycleSubject = PublishSubject.create();

    protected abstract void initData();//处理数据

    /*
    * 防止getActivity为空
    *
    * */
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mActivity = getActivity();

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//
        rootView = inflater.inflate(provideContentViewId(), container, false);
        unbinder = ButterKnife.bind(this, rootView);
        injectView(rootView);
        initData();
        return rootView;
    }


    public void injectView(View rootView){

    }




    @Override
    public void onDestroyView() {
        super.onDestroyView();
        lifecycleSubject.onNext(ActivityLifeCycleEvent.DESTROY);
        unbinder.unbind();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

    }

    /**
     * @date 创建时间：2017/8/6
     * @author cmw
     * @description 布局id
     */
    protected abstract int provideContentViewId();


    protected boolean isNetworkConnected(Context context) {
        if (context != null) {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
            if (mNetworkInfo != null) {
                return mNetworkInfo.isAvailable();
            }
        }
        return false;
    }


    @Override
    public void onPause() {
        super.onPause();
        lifecycleSubject.onNext(ActivityLifeCycleEvent.PAUSE);

    }


    /**
     * @date 创建时间：2017/8/6
     * @author cmw
     * @description 吐司
     */
    protected void showToast(String msg) {
        if (msg == null) return;
        MyToast.show(mActivity, msg);
    }


    //    跳转
    protected void intentActivity(Class<? extends Activity> intentActivity) {
        Intent intent = new Intent(mActivity, intentActivity);
        startActivity(intent);
    }

    /**
     * @description 注册evenbus
     */
    protected void registEventBus() {
        EventBus.getDefault().register(this);
    }

    /**
     * @description 注销evenbus
     */
    protected void unRegistEventBus() {
        EventBus.getDefault().unregister(this);
    }



    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        mActivity.getWindow().setAttributes(lp);
    }

}
